fix(api): validate GitHub token before GraphQL requests#379
Conversation
|
@adnankhan46 is attempting to deploy a commit to the AJEET PRATAP SINGH's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe project service refactors GitHub API client initialization from module-level setup to on-demand creation via helper functions. Token retrieval now validates and trims ChangesGitHub Client Authentication Refactor
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/api/src/services/project.service.ts`:
- Line 67: The code currently calls createGithubClient() inside
fetchGithubProjects, recreating graphqlWithAuth on every request; change this by
introducing a module-scoped cached client (e.g., a let cachedGraphqlWithAuth)
and modify fetchGithubProjects to initialize cachedGraphqlWithAuth =
createGithubClient() only if it’s undefined or if the token changed/invalid,
otherwise reuse the cached instance; ensure token validation and clear error
handling during first creation so subsequent calls use the pooled client instead
of recreating graphqlWithAuth each time.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f2d61609-8108-4d5b-afaf-b45ce2ac9751
📒 Files selected for processing (1)
apps/api/src/services/project.service.ts
| queryParts.push(`fork:true`); | ||
|
|
||
| const searchQueryString = queryParts.join(" "); | ||
| const graphqlWithAuth = createGithubClient(); |
There was a problem hiding this comment.
Avoid recreating the GitHub client on every request.
Creating a new client instance on every call to fetchGithubProjects violates the connection pooling guideline for external service clients and introduces unnecessary performance overhead. Consider caching the client instance after first creation.
As per coding guidelines: apps/api/src/**/{database,clients,services}/*.{js,ts}: Use connection pooling for database and external service clients.
⚡ Proposed fix to cache the client instance
+let cachedClient: ReturnType<typeof graphql.defaults> | null = null;
+
-const createGithubClient = () =>
- graphql.defaults({
- headers: {
- authorization: `token ${getGithubPersonalAccessToken()}`,
- },
- });
+const createGithubClient = () => {
+ if (!cachedClient) {
+ const token = getGithubPersonalAccessToken();
+ cachedClient = graphql.defaults({
+ headers: {
+ authorization: `token ${token}`,
+ },
+ });
+ }
+ return cachedClient;
+};This approach validates the token on first call and reuses the client for subsequent requests, improving performance while maintaining clear error handling.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@apps/api/src/services/project.service.ts` at line 67, The code currently
calls createGithubClient() inside fetchGithubProjects, recreating
graphqlWithAuth on every request; change this by introducing a module-scoped
cached client (e.g., a let cachedGraphqlWithAuth) and modify fetchGithubProjects
to initialize cachedGraphqlWithAuth = createGithubClient() only if it’s
undefined or if the token changed/invalid, otherwise reuse the cached instance;
ensure token validation and clear error handling during first creation so
subsequent calls use the pooled client instead of recreating graphqlWithAuth
each time.
Summary
GITHUB_PERSONAL_ACCESS_TOKENtoken undefinedfix for #378
Summary by CodeRabbit